<template>
  <div class="list-container">
    <el-card class="box-card">
      <div class="clearfix">
        <div class="carheader">
          <span class="explain">说明：目前支持学科和关键字条件筛选</span>
          <el-button
            style="float: right"
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="$router.push('new')"
            >新增试题</el-button
          >
        </div>
        <el-form ref="sizeForm" label-width="80px" size="small">
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  @change="SubjectSearch"
                  v-model="sizeForm.subject"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in subjectlist"
                    :label="item.label"
                    :key="index"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select v-model="sizeForm.second" placeholder="请选择">
                  <el-option
                    v-for="(item, index) in directoryOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select v-model="sizeForm.tag" placeholder="请选择">
                  <el-option
                    v-for="item in tagOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  v-model="searchInput"
                  placeholder="根据题干搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select v-model="sizeForm.genre" placeholder="请选择">
                  <el-option v-for="item in questionTypelist" :key="item.value" :value="item.value" :label="item.label"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select v-model="sizeForm.difficulty" placeholder="请选择">
                  <el-option v-for="item in difficultylist" :key="item.value" :value="item.value" :label="item.label"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select v-model="sizeForm.direction" placeholder="请选择">
                  <el-option v-for="item in directionList" :key="item.value" :value="item.value" :label="item.label"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select v-model="sizeForm.entering" placeholder="请选择">
                  <el-option label="区域一" value="shanghai"></el-option>
                  <el-option label="区域二" value="beijing"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  style="width: 48%; margin-right: 2%"
                  v-model="sizeForm.city"
                  placeholder="请选择"
                  @change="handleCity"
                >
                  <el-option
                    v-for="(item, index) in optionsprovinces"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <el-select
                  style="width: 50%"
                  v-model="sizeForm.county"
                  placeholder="请选择"
                >
                  <el-option v-for="(item, index) in childrencitys" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <div style="float: right">
                  <el-button @click="cancleclear()">取消</el-button>
                  <el-button type="primary" @click="onSubmit">搜索</el-button>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-alert
          style="margin-bottom: 15px"
          :title="`数据一共 ${total} 条`"
          type="info"
          show-icon
        >
        </el-alert>

        <el-table :data="items" style="width: 100%">
          <el-table-column prop="number" label="试题编号" width="120">
          </el-table-column>
          <el-table-column prop="subject" label="学科" width="220">
          </el-table-column>
          <el-table-column prop="catalog" label="目录" width="225">
          </el-table-column>
          <el-table-column label="题型" width="210">
            <template slot-scope="scope">
              <span v-if="scope.row.questionType == 1">单选</span>
              <span v-else-if="scope.row.questionType == 2">多选</span>
              <span v-else>简答</span>
            </template>
          </el-table-column>
          <el-table-column label="题干" width="285">
             <template slot-scope="scope">
              <span v-html="scope.row.question"></span>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="180"> 
            <template slot-scope="scope">
              {{scope.row.addDate | parseTimeByString}}
            </template>
          </el-table-column>
          <el-table-column label="难度" width="230">
            <template slot-scope="scope">
              <span v-if="scope.row.difficulty == 1">简单</span>
              <span v-else-if="scope.row.difficulty == 2">普通</span>
              <span v-else>困难</span>
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" width="215">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
              type="primary"
              icon="el-icon-view"
              size="small"
              plain
              circle
              @click="preivew(scope.row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              plain
              circle
              @click="$router.push(`new?id=${scope.row.id}`)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              plain
              circle
              @click="clear(scope.row)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              size="small"
              plain
              circle
              @click="addChoice(scope.row)"
            ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 预览弹窗 -->
      <questions-preview :data="questionInfo" :isShowDialog="isShowDialog" @closeDialog="closeDialog"/>
    </el-card>
  </div>
</template>

<script>
import { list as questions,remove, choiceAdd } from "@/api/hmmm/questions";
import { simple } from "@/api/hmmm/subjects";
import { provinces,citys } from "@/api/hmmm/citys";
import {simple as directoryList} from '@/api/hmmm/directorys'
import { simple as tagList } from '@/api/hmmm/tags'
import {difficulty,questionType,direction} from '@/api/hmmm/constants'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  components: {
    QuestionsPreview
  },
  data() {
    return {
      difficulty,
      questionType,
      direction,
      value: "",
      sizeForm: {
        subject: null,
        second: null,
        tag: null,
        keyword: null,
        genre: null,
        difficulty: null,
        direction: null,
        entering: null,
        city: null,
        county: null,
      },
      subjectlist: null,
      total: null,
      queryInfo: {
        page: 1,
        pagesize: 5,
      },
      items: [
        {
          id:"",
          number: "",
          subject: "",
          catalog: "",
          questionType: "",
          question: "",
          addDate: "",
          difficulty: "",
          creator: "",
        },
      ],
      subjectID: null,
      searchInput: "",
      optionsprovinces: provinces(),
      childrencitys:null,
      dialogVisible: false,
      previewlist:null,
      difficultylist:difficulty,
      questionTypelist:questionType,
      directionList:direction,
      directoryOptions:null,
      tagOptions:null,
      questionInfo:null,
      isShowDialog: false
    };
  },
  created() {
    this.loadquestions();
    this.loadsubject();
  },
  filters: {
      
  },
  methods: {
    // 页面总列表加载
    async loadquestions() {
      try {
        const { data: data } = await questions(this.queryInfo);
        // console.log(data);
        this.items = data.items;
        this.total = data.counts;
        this.queryInfo.pagenum = data.page;
      } catch (error) {
        this.$message.error("获取信息失败");
      }
    },
    // 加载页面搜索列表
    async loadsubject() {
      const { data: res } = await simple();
      this.subjectlist = res;
    },
    // 监听 每页显示多少条
    handleSizeChange(size) {
      this.queryInfo.page = 1
      this.queryInfo.pagesize = size;
      this.loadquestions();
    },
    // 监听当前第几页
    handleCurrentChange(np) {
      this.queryInfo.page = np;
      this.loadquestions()
    },
    // 学科选择框
    async SubjectSearch(subjectID) {
      // console.log(val);
      this.subjectID = subjectID;
      if(subjectID) {
        const {data:res} = await directoryList({subjectID})
        this.directoryOptions = res
        const { data: tagArr } = await tagList({ subjectID })
        this.tagOptions = tagArr
      } else {
        this.directoryOptions = []
        this.tagOptions = []
      }
    },
    async onSubmit() {  
      if (this.subjectID) {
        this.items = [];
        const subjectID = this.subjectID;
        const { data: data } = await questions({ subjectID });
        this.items = data.items
      } else if(this.searchInput){
        console.log(this.searchInput);
        this.items = []
        const keyword = this.searchInput
        const {data:res} = await questions({keyword})
        this.items = res.items
      }
    },
    // 城市选择器
    handleCity(val) {
      if(val) {
        this.childrencitys = citys(val)
      } else {
        this.childrencitys = []
      }
    },
    // 预览按钮
    preivew(questionInfo){
      this.questionInfo = questionInfo
      this.isShowDialog = true
    },
    // 删除按钮
    clear(val) {
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => { 
           await remove(val)
          this.$message.success('删除成功');
          setTimeout(()=>{
            this.loadquestions()
          },200)
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 加入精选
      addChoice(val) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(async ()=>{
            await choiceAdd({id:val.id,choiceState:1})
            this.$message.success('加入精选题库成功')
            setTimeout(()=>{
            this.loadquestions()
          },200)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 弹窗 
    closeDialog() {
      this.isShowDialog = false
    },
    // 取消
    cancleclear() {
      this.sizeForm = []
    },
  },
};
</script>

<style lang="less" scoped>
.list-container {
  padding: 10px;
  .explain {
    color: pink;
    font-size: 12px;
  }
  .carheader {
    height: 30px;
    margin-bottom: 15px;
  }
  .el-row {
    .el-col {
      width: 25%;
    }
    .el-form-item {
      margin-bottom: 18px;
    }
    .el-form-item__label {
      text-align: right;
      vertical-align: middle;
      float: left;
      font-size: 14px;
      line-height: 40px;
      padding: 0 12px 0 0;
    }
    .el-select {
      width: 100%;
    }
  }
  .el-pagination {
    float: right;
    margin: 20px;
  }
}
</style>